<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\ConfigContent as configContentModel;

/**
 * 配置软文控制器
 * @package app\user\admin
 */
class Software extends Admin
{
    /**
     * 支付种类列表
     * @author 李洪波 <361951119@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data_list = configContentModel::where($this->getMap())
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setTableName('config_content')
            ->addTopButton('add',['title'=>'Add'])
            ->addRightButton('edit',['title'=>'edit'])
            ->addRightButton('delete',['title'=>'delete','data-confirm'=>'confirm','data-cancel'=>'cancel','data-title'=>'Are you sure you want to perform this operation?'])
            ->addColumns([
                ['id', 'ID'],
                ['key', 'Type'],
                ['updater', 'Updater'],
                ['update_time', 'Last update time'],
                ['right_button', 'Operation', 'btn'],
            ])
            ->setRowList($data_list)
            ->setSearchArea([
                ['text', 'key', 'Type','like'],
            ])
            ->fetch();
    }

    /**
     * 新增
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['key'] == '' && $this->error('Please fill in the key ');
            $data['content'] == '' && $this->error('Please fill in the content ');
            if(!$this->check($data['key'])) $this->error('The type value cannot be duplicate');
            $list = [];
            $list['key']=$data['key'];
            $list['content']=$data['content'];
            $list['update_time']=date('Y-m-d H:i:s');
            $list['create_time']=date('Y-m-d H:i:s');
            $list['updater']=session('user_auth')['username'];
            $contentModel = new configContentModel();
            if (false !== $contentModel->save($list)) {
                $this->success('add success', 'index');
            } else {
                $this->error('add failed');
            }
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'key', 'Type'],
            ])->addUeditor('content','Content')
            ->fetch();
    }

    public function edit($id = 0,$pid=0)
    {
        if ($id === 0) $this->error('missing parameter');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post('', null, 'trim');
            // 验证

            $data['key'] == '' && $this->error('Please fill in the key ');
            $data['content'] == '' && $this->error('Please fill in the content ');
            if(!$this->check($data['key'],$id)) $this->error('The type value cannot be duplicate');
            $data['update_time']=date('Y-m-d H:i:s');
            $data['updater']=session('user_auth')['username'];
            if (configContentModel::update($data,['id'=>$id])) {
                $this->success('edit success', 'index');
            } else {
                $this->error('edit failed');
            }
        }
        // 获取数据
        $info = configContentModel::get($id);

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'key', 'Type'],
            ])->addUeditor('content','Content')
            ->setFormData($info)
            ->fetch();
    }


    public function delete($record = [])
    {
        $ids   = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        if (configContentModel::where(['id'=>$ids])->delete()) {
            $this->success('delete success', 'index');
        } else {
            $this->error('delete failed');
        }
    }

    private function check($name,$id=0){
        $id=configContentModel::where(['key'=>$name])
            ->when($id,function ($query)use($id){
                $query->where('id','neq',$id);
            })
            ->value('id');
        if($id){
            return false;
        }else{
            return true;
        }
    }
}
